from Structure import Stack

# 用栈思想解决匹配符号问题 LIFO Last In First Out
def parChecker2(symbolString):
    s = Stack()
    balanced = True
    index = 0
    while index < len(symbolString) and balanced:
        symbol = symbolString[index]
        if symbol in "([{":
            s.push(symbol)
        else:
            if s.isEmpty():
                balanced =  False
            else:
                top = s.pop()
                if not matches(top, symbol):
                    balanced = False

        index = index + 1

    if balanced and s.isEmpty():
        return True
    else:
        return False

def matches(open, close):
    opens = "([{"
    closers = ")]}"

    return opens.find(open) == closers.find(close) # find 相较于 index 不会报异常

string = '({}[])'
if (parChecker2(string)):
    print("well done")
else:
    print("something wrong!")